﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Principal;
using System.Threading;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;

using AMPLibrary;
using AMPLibrary.Media;
using AMPLibrary.Controllers;
using AMPLibrary.Collections;
using AMPLibrary.Windows;


namespace MediaLibrary
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application, ISingleInstanceApp
    {
        /// <summary>
        /// 
        /// </summary>
        [STAThread]
        public static void Main()
        {
            if (SingleInstance<App>.InitializeAsFirstInstance("thisismyunuqiestringeedstytampexe"))
            {
                var application = new App();

                MediaLibrary.MainWindow mainWindow = MediaLibrary.MainWindow.GetMainWindow();
                mainWindow.HandleCommandLineArgs(SingleInstance<App>.CommandLineArgs);

                application.Run(mainWindow);
                
                // Allow single instance code to perform cleanup operations
                SingleInstance<App>.Cleanup();
            }
        }

        /// <summary>
        /// Application Entry Point
        /// 
        /// - Sets up the theme to be used by the application
        /// - gives names to the settings files
        /// - sets the applications working directory
        /// - sets up the applications settings if this is the first time the program has been run
        /// - wires up some event handlers
        /// </summary>
        public App()
        {
            OperatingSystem OperatingSystem = System.Environment.OSVersion;
            WindowsIdentity WindowsIdentity = WindowsIdentity.GetCurrent();

            SplashScreen ss = new SplashScreen("resources/SplashScreen.bmp");
            ss.Show(true);

            if (OperatingSystem.Platform != PlatformID.Win32NT)
            {
                MessageBox.Show("AMP must run a Windows NT Platform running v4.0 of the .NET Framework. (Windows 2000 or higher)", "AMP Error", MessageBoxButton.OK, MessageBoxImage.Error);
                Application.Current.Shutdown();
            }

            AMPLibrary.Paths.SetPaths();
            AMPLibrary.Settings.LoadSettings();

            InitializeComponent();
                        
            ExcludedMediaCollection excludedMediaCollection = ExcludedMediaCollection.GetExcludedMediaCollection();
            excludedMediaCollection.Deserialize(Paths.ExcludedMediaCollectionSaveFile);

            PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
            playlistCollection.Deserialize(Paths.PlaylistCollectionSaveFile);

            AutomaticPlaylistCollection automaticPlaylistCollection = AutomaticPlaylistCollection.GetAutomaticPlaylistCollection();
            automaticPlaylistCollection.Deserialize(Paths.AutomaticPlaylistCollectionSaveFile);

            MusicCollection musicCollection = MusicCollection.GetMusicCollection();
            musicCollection.Deserialize(Paths.MusicCollectionSaveFile);

            VideoCollection videoCollection = VideoCollection.GetVideoCollection();
            videoCollection.Deserialize(Paths.VideoCollectionSaveFile);

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.Deserialize(Paths.NowPlayingSaveFile);

            WatchFolderController watchFolderController = WatchFolderController.GetWatchFolderController();
            watchFolderController.Start(Settings.WatchFolderScanMode);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool SignalExternalCommandLineArgs(IList<string> args)
        {
            MediaLibrary.MainWindow.GetMainWindow().HandleCommandLineArgs(args);

            return true;
        }

        /// <summary>
        /// Event handler, fired when the application is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void App_Startup(object sender, StartupEventArgs e)
        {
        }

        /// <summary>
        /// Event handler, fired when the application is being closed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void App_Exit(object sender, ExitEventArgs e)
        {
            // save the settings and playlist so that when the next time the application is run they will be saved
            if (!AMPLibrary.Settings.SaveSettings())
                MessageBox.Show("AMP Failed to save your settings from this session.", "AMP Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        
            WatchFolderController watchFolderController = WatchFolderController.GetWatchFolderController();
            watchFolderController.Stop();
            watchFolderController.Dispose();

            ExcludedMediaCollection excludedMediaCollection = ExcludedMediaCollection.GetExcludedMediaCollection();
            excludedMediaCollection.Serialize(Paths.ExcludedMediaCollectionSaveFile);

            PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
            playlistCollection.Serialize(Paths.PlaylistCollectionSaveFile);

            AutomaticPlaylistCollection automaticPlaylistCollection = AutomaticPlaylistCollection.GetAutomaticPlaylistCollection();
            automaticPlaylistCollection.Serialize(Paths.AutomaticPlaylistCollectionSaveFile);            

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.Serialize(Paths.NowPlayingSaveFile);

            MusicCollection musicCollection = MusicCollection.GetMusicCollection();
            musicCollection.Serialize(Paths.MusicCollectionSaveFile);

            VideoCollection videoCollection = VideoCollection.GetVideoCollection();
            videoCollection.Serialize(Paths.VideoCollectionSaveFile);
        }

        /// <summary>
        /// Method, loads the applications default settings
        /// </summary>
        private void LoadDefaults()
        {
            /* an make sure the playlist is clear */
            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();
        }
    }
}